home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / util / pack / xpk_Source.lha / xpk_Source / shell / xUp.c < prev   
C/C++ Source or Header  |  1998-11-09  |  4KB  |  170 lines

  1. #define NAME        "xUp"
  2. #define DISTRIBUTION    "(Freeware) "
  3. #define REVISION    "4"
  4.  
  5. /* Programmheader
  6.  
  7.     Name:        xUp
  8.     Author:        SDI (before 1.1 Urban Dominik Müller)
  9.     Distribution:    Freeware
  10.     Description:    General XPK file-to-file unpacker
  11.     Compileropts:    -
  12.     Linkeropts:    -l xpkmaster
  13.  
  14.  1.1   09.10.96 : fixed error with new 3.10 xpkmaster.library (A4 problem)
  15.  1.2   29.11.96 : recompiled
  16.  1.3   14.02.97 : corrected suffix removement
  17.  1.4   28.11.97 : fixed bug in chunk-hook
  18. */
  19.  
  20. #include "SDI_defines.h"
  21. #define SDI_TO_ANSI
  22. #include "SDI_ASM_STD_protos.h"
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>
  25. #include <proto/xpkmaster.h>
  26.  
  27. #ifdef __MAXON__
  28.   #define __asm
  29.   #define __saveds
  30. #endif
  31.  
  32. struct Library *XpkBase = 0;
  33.  
  34. UBYTE errbuf[XPKERRMSGSIZE + 1];    /* +1 to make room for '\n'*/
  35.  
  36. ULONG __asm __saveds chunkfunc(register __a1 struct XpkProgress *prog)
  37. {
  38.   ULONG i;
  39.  
  40.   if(prog->xp_Type == XPKPROG_START)
  41.     printf("\033[0 p");
  42.  
  43.   if(prog->xp_Type != XPKPROG_END)
  44.     printf("\r%4s: %-8s (%3ld%% done, %2ld%% CF, %6ld cps) %s\033[K",
  45.        prog->xp_PackerName, prog->xp_Activity, prog->xp_Done,
  46.        prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  47.   else
  48.     printf("\r%4s: %-8s (%3ldK, %2ld%% CF, %6ld cps) %s\033[K\n",
  49.        prog->xp_PackerName, prog->xp_Activity, prog->xp_ULen >> 10,
  50.        prog->xp_CF, prog->xp_Speed, prog->xp_FileName);
  51.  
  52.   if(prog->xp_Type == XPKPROG_END)
  53.     printf("\033[1 p");
  54.  
  55.   if((i = SetSignal(0, SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C))
  56.     printf("\n");
  57.   return i;
  58. }
  59.  
  60. struct Hook chunkhook = {{0}, (ULONG (*) ()) chunkfunc};
  61.  
  62. UBYTE namebuf[200];
  63.  
  64. STRPTR tempname(STRPTR name)
  65. {
  66.   ULONG i = strlen(name);
  67.   CopyMem(name, namebuf, i);
  68.   for(name = namebuf + i; name > namebuf; name--)
  69.     if(name[-1] == '/' || name[-1] == ':')
  70.       break;
  71.  
  72.   sprintf(name, "tmp%lx", &name);
  73.   return namebuf;
  74. }
  75.  
  76. void end(STRPTR text)
  77. {
  78.   if(text)    printf(text);
  79.   if(XpkBase)    CloseLibrary(XpkBase);
  80.   exit(text ? RETURN_ERROR : 0);
  81. }
  82.  
  83. void main(int argc, char **argv)
  84. {
  85.   LONG res, i, suffix, len, sufmode = 0;
  86.   STRPTR password = 0;
  87.  
  88.   if(!(XpkBase = OpenLibrary(XPKNAME, 0)))
  89.     end("Cannot open " XPKNAME "\n");
  90.  
  91.   if(argc == 1 || !strcmp (argv[1], "?"))
  92.     end("Usage: " NAME " [-s|-S] [-p password] files\n");
  93.  
  94.   for(i = 1; i + 1 < argc; ++i) /* find parameters */
  95.   {
  96.     if(!strcmp(argv[i], "-s"))
  97.       sufmode = 1;
  98.     else if(!strcmp(argv[i], "-S"))
  99.       sufmode = 2;
  100.     else if(!strcmp(argv[i], "-p"))
  101.       password = argv[++i];
  102.     else
  103.       break;
  104.   }
  105.  
  106.   for(; i < argc; i++)
  107.   {
  108.     tempname(argv[i]);
  109.     len = strlen(argv[i]);
  110.     suffix = 0;
  111.     if(sufmode == 1 && len > 4 && !stricmp(argv[i] + len - 4, ".xpk"))
  112.     {
  113.       CopyMem(argv[i], namebuf, len-4);
  114.       namebuf[len-4] = 0;
  115.       suffix = 1;
  116.     }
  117.     else if(sufmode == 2 && len > 2)
  118.     {
  119.       char *end = argv[i] + len-1;
  120.  
  121.       while(end > argv[i])
  122.       {
  123.         if(*end == '.')
  124.         {
  125.       CopyMem(argv[i], namebuf, end-argv[i]);
  126.       namebuf[end-argv[i]] = 0;
  127.           suffix = 1; break;
  128.         }
  129.         --end;
  130.       }
  131.     }
  132.  
  133.     if((res = XpkUnpackTags(
  134.     XPK_InName, (ULONG) argv[i],
  135.     XPK_FileName, (suffix ? namebuf : argv[i]),
  136.     XPK_OutName, (ULONG) namebuf,
  137.     XPK_ChunkHook, (ULONG) &chunkhook,
  138.     XPK_Password, (ULONG) password,
  139.     XPK_GetError, (ULONG) errbuf,
  140.     XPK_NoClobber, TRUE,
  141.     TAG_DONE
  142.     )) && res != XPKERR_NOTPACKED)
  143.     {
  144.       if(errbuf)
  145.       {
  146.         ULONG i = strlen(errbuf);
  147.         errbuf[i++] = '\n';
  148.         errbuf[i] = 0;
  149.       }
  150.       end(errbuf);
  151.     }
  152.  
  153.     if(res)
  154.     {
  155.       printf("%s\n", errbuf);
  156.       if(!DeleteFile(namebuf))
  157.     end("Cannot delete outfile\n");
  158.     }
  159.  
  160.     else if(!suffix)
  161.     {
  162.       if(!DeleteFile(argv[i]))
  163.     end("Cannot delete input file\n");
  164.       if(!Rename(namebuf, argv[i]))
  165.     end("Cannot rename tempfile\n");
  166.     }
  167.   }
  168.   end(0);
  169. }
  170.